<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:exsl="http://exslt.org/common"
    xmlns:ep="urn:ep">

    <xsl:import href="ep_component.xsl" />
    <xsl:import href="ep_menu.xsl" />
    <xsl:import href="ep_dataset_explorer.xsl" />
    <!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -->
    <xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" indent="yes" />

    <xsl:param name="cgi_href" />
    <xsl:param name="html_href" />
    <xsl:param name="core_href" />

    <!-- user_level variable: 0 - basic, 1 - intermediate, 2 - expert (not used now) -->
    <xsl:param name="user_level" />
    <xsl:param name="session_id" />
    <xsl:param name="epc_menu_uri" />
    <xsl:param name="session_duration" />
    <xsl:param name="VERSION" />
    <xsl:param name="show_context_help">1</xsl:param>
    <xsl:param name="epc_size_state">0</xsl:param>

    <xsl:variable name="query" select="ep:get_epc_query()" />

    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
            <head>
                <title>EBI Expression Profiler</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <meta name="description" content="The European Bioinformatics Institute" />
                <meta name="author" content="EBI Web Team" />
                <meta http-equiv="Content-Language" content="en-GB" />
                <meta http-equiv="Window-target" content="_top" />
                <meta name="no-email-collection" content="http://www.unspam.com/noemailcollection/" />
                <link rel="stylesheet" href="http://www.ebi.ac.uk/inc/css/contents.css" type="text/css" />
                <link rel="stylesheet" href="http://www.ebi.ac.uk/inc/css/userstyles.css" type="text/css" />
                <script src="http://www.ebi.ac.uk/inc/js/contents.js" type="text/javascript"></script>
                <link rel="SHORTCUT ICON" href="http://www.ebi.ac.uk/bookmark.ico" />
                <link rel="stylesheet" href="{$html_href}/static/css/ep_ui.css" type="text/css" />
<!--            <link rel="stylesheet" href="{$html_href}/static/css/ep_tabs.css" type="text/css" /> -->
                <xsl:comment>[if lte IE 6]&gt;&lt;link rel="stylesheet" type="text/css" href="<xsl:value-of select="$html_href" />/static/css/ep_ie6_fixes.css" /&gt;&lt;![endif]</xsl:comment>
                <script src="{$html_href}/static/js/jquery.1.1.1.js" type="text/javascript"></script>
                <script src="{$html_href}/static/js/jqbrowser.js" type="text/javascript"></script>
                <script src="{$html_href}/static/js/ep_ui.js" type="text/javascript"></script>
                <script src="{$html_href}/static/js/ep_tabs.js" type="text/javascript"></script>
                <script src="{$html_href}/static/js/ep_xml.js" type="text/javascript"></script>

<!--
                <link href="{$html_href}/static/css/ep_tools_ui.css" rel="stylesheet" type="text/css"/>
                <link href="{$html_href}/static/css/stylesheet.css" rel="stylesheet" type="text/css"/>
-->
                <script type="text/javascript">
                    var session_duration = 60 * <xsl:value-of select="$session_duration" />;
                    var show_context_help = <xsl:value-of select="$show_context_help" />;
                    var session_id = "<xsl:value-of select="$session_id" />";
                    var cgi_href = "<xsl:value-of select="$cgi_href" />";
                    var html_href = "<xsl:value-of select="$html_href" />";
                    var core_href = "<xsl:value-of select="$core_href" />";
                </script>
<!--
                <script src="{$html_href}/static/js/ep_xml.js" type="text/javascript"/>
                <script src="{$html_href}/static/js/dtree.js" type="text/javascript"></script>

                <script src="{$html_href}/static/js/ep_ui.js" language="JavaScript"></script>
                <script src="{$html_href}/static/js/tip.js" language="JavaScript"></script>   
                <script src="{$html_href}/static/js/floatbox.js" language="JavaScript"></script>
                <script type="text/javascript">
                    function about_help ()
                        { 
                    alert( document.getElementById('epngversion').innerHTML + "\nDeveloped by Microarray Informatics Team, EBI\nThis is open-source software with multiple contributors\nCopyright 1999-2006");
                        }
                </script>
-->
            </head>
            <body> <!-- onload="if(navigator.userAgent.indexOf('MSIE') != -1) document.getElementById('head').allowTransparency = true"> -->
                <div id="ep_container" style="z-index: 566">
                    <div id="ep_status_window">
                        <div id="ep_status_text">Please wait...</div>
                    </div>
                    <div id="ep_workspace" class="column">
                        <div class="ep_component_error" style="display: none" /><!-- that's a nasty IE6 hack to display errors properly -->
                        <form id="ep_main_form" name="mainfrm" method="post" enctype="multipart/form-data" action="{$cgi_href}/ep_ui.pl">
                            <input type="hidden" id="ep_session_id" name="session_id" value="{$session_id}" />
                            <input type="hidden" id="ep_active_component_id" name="active_component_id" value="" />
                            <input type="hidden" id="ep_kill_session" name="kill_session" value="" />
                            <xsl:variable name="active_component_id" select="$query//component[@active=1]/@id"/>
                            <xsl:choose>
                                <xsl:when test="string-length($active_component_id)!=0">
                                    <xsl:call-template name="process_component_chain">
                                        <xsl:with-param name="this_component_id" select="$active_component_id" />
                                        <xsl:with-param name="doc" select="/"/>
                                    </xsl:call-template>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:variable name="first_comp_id" select="//component[position()=1]/@id"/>
                                    <xsl:call-template name="render_component">
                                        <xsl:with-param name="component" select="//component[@id=$first_comp_id]"/>
                                        <xsl:with-param name="query" select="$query//component[@id=$first_comp_id]"/>
                                        <xsl:with-param name="epc_size_state" select="$epc_size_state"/>
                                    </xsl:call-template>
                                    <input type="hidden" id="ep_render_component_ids" name="render_component_ids" value="{//component[position()=1]/@id}" />
                                </xsl:otherwise>
                            </xsl:choose>
                        </form>
                    </div>
                    <xsl:variable name="user_name" select="ep:get_username()" />
                    <xsl:if test="string-length($user_name)&gt;0">
                        <xsl:call-template name="render_ep_menu">
                            <xsl:with-param name="epc_menu" select="document($epc_menu_uri)" />
                            <xsl:with-param name="user_name" select="$user_name" />
                            <xsl:with-param name="user_level" select="$user_level" />
                        </xsl:call-template>
                        <script type="text/javascript">enable_ep_menu_on_document_ready();</script>
                    </xsl:if>
                </div>
<!--
                <div class="headerdiv" id="headerdiv" style="position:absolute; z-index: 1; height: 57px">
                    <iframe src="http://www.ebi.ac.uk/inc/head.html" name="head" id="head" frameborder="0" marginwidth="0px" marginheight="0px" scrolling="no" width="100%" style="position:absolute; z-index: 1; height: 57px;"></iframe>
                </div>
-->
<!-- TODO
                <table class="footerpane" id="footerpane" summary="The main footer pane of the page">
                    <tr>
                        <td colspan ="4" class="footerrow">
                            <div class="footerdiv" id="footerdiv"  style="z-index:2;">
                                <iframe src="http://www.ebi.ac.uk/inc/foot.html" name="foot" frameborder="0" marginwidth="0px" marginheight="0px" scrolling="no"  height="22px" width="100%"  style="z-index:2;"/>
                            </div>
                        </td>
                    </tr>
                </table>
                <script src="http://www.ebi.ac.uk/inc/js/footer.js" type="text/javascript"></script>
-->
            </body>
        </html>
    </xsl:template>

    <xsl:template name="process_component_chain">
        <xsl:param name="this_component_id"/>
        <xsl:param name="this_target"/>
        <xsl:param name="this_component_input"/>

        <xsl:param name="doc"/>
        <xsl:param name="interactive" select="1"/>

        <xsl:if test="$query//component[@id=$this_component_id]/@is_valid=1">

            <!-- component process status: OK for success, error code otherwise -->
            <xsl:variable name="process_status" select="ep:process_component(string($this_component_id), string(exsl:node-set($this_component_input)/@comp_id))"/>
            <xsl:variable name="this_component_output" select="ep:component_output(string($this_component_id))"/>

            <xsl:if test="$process_status='OK'">
                <!-- render this (just processed) component -->
                <xsl:if test="$interactive!=1">
                    <xsl:call-template name="render_component">
                        <xsl:with-param name="component" select="$doc//component[@id=$this_component_id]"/>
                        <xsl:with-param name="target" select="$this_target"/>
                        <xsl:with-param name="query" select="$query//component[@id=$this_component_id]"/>
                        <xsl:with-param name="epc_input" select="$this_component_input"/>
                        <xsl:with-param name="minimize">
                            <xsl:if test="$doc//component[@id=$this_component_id]/action/minimize_on_success/text()=1">1</xsl:if>
                        </xsl:with-param>
                        <xsl:with-param name="epc_size_state" select="$epc_size_state"/>
                        <xsl:with-param name="message">Component processed.</xsl:with-param>
                    </xsl:call-template>
                </xsl:if>
    
                <!-- test if there are more targets to follow -->
                <xsl:choose>
                    <xsl:when test="count($query//component[@active=1]/target_sequence/target[@comp_id=$this_component_id]/following-sibling::*) &gt; 1">
                        <xsl:variable name="next_component_id" select="$query//component[@active=1]/target_sequence/target[@comp_id=$this_component_id]/following-sibling::*[1]/@comp_id"/>
                        <xsl:variable name="next_target" select="$query//component[@active=1]/target_sequence/target[@comp_id=$this_component_id]/following-sibling::*[1]"/>
    
                        <!-- launch the processing of the following components -->
                        <xsl:call-template name="process_component_chain">
                            <xsl:with-param name="this_component_id" select="$next_component_id"/>
                            <xsl:with-param name="this_target" select="$next_target"/>
                            <xsl:with-param name="this_component_input" select="$this_component_output"/>
                            <xsl:with-param name="doc" select="$doc"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <!-- next-to-last target was processed - just render the last one --> 
                        <xsl:variable name="last_component_id" select="$query//component[@active=1]/target_sequence/target[position()=last()]/@comp_id"/>
                        <xsl:variable name="last_target" select="$query//component[@active=1]/target_sequence/target[position()=last()]"/>
    
                        <xsl:call-template name="render_component">
                            <xsl:with-param name="component" select="$doc//component[@id=$last_component_id]"/>
                            <xsl:with-param name="target" select="$last_target"/>
                            <xsl:with-param name="query" select="$query//component[@id=$last_component_id]"/>
                            <xsl:with-param name="epc_input" select="$this_component_output"/>
                            <xsl:with-param name="epc_size_state" select="$epc_size_state"/>
                            <xsl:with-param name="maximize" select="1"/>
                            <xsl:with-param name="prev_component" select="$doc//component[@id=$this_component_id]"/>
                        </xsl:call-template>

                        <input type="hidden" id="ep_render_component_ids" name="render_component_ids">
                            <xsl:attribute name="value"><xsl:value-of select="$last_component_id"/></xsl:attribute>
                        </input>

                    </xsl:otherwise>
                </xsl:choose>
            </xsl:if>

            <xsl:if test="$process_status!='OK'">
                <input type="hidden" name="EPC_not_processed_OK">
                    <xsl:attribute name="value"><xsl:value-of select="$this_component_id"/></xsl:attribute>
                </input>

                <xsl:variable name="component_errors" select="ep:component_errors(string($this_component_id))"/>

                <xsl:call-template name="render_component">
                    <xsl:with-param name="component" select="$doc//component[@id=$this_component_id]"/>
                    <xsl:with-param name="target" select="$this_target"/>
                    <xsl:with-param name="query" select="$query//component[@id=$this_component_id]"/>
                    <xsl:with-param name="epc_input" select="$this_component_output"/>
                    <xsl:with-param name="epc_size_state" select="$epc_size_state"/>
                    <xsl:with-param name="message" select="$component_errors"/>
                </xsl:call-template>
        
                <input type="hidden" id="ep_render_component_ids" name="render_component_ids">
                    <xsl:attribute name="value"><xsl:value-of select="$this_component_id"/></xsl:attribute>
                </input>
            </xsl:if>
    
        </xsl:if>

        <xsl:if test="$query//component[@id=$this_component_id]/@is_valid=0">
            <input type="hidden" name="EPC_not_processed_OK">
                <xsl:attribute name="value"><xsl:value-of select="$this_component_id"/></xsl:attribute>
            </input>
            <xsl:call-template name="render_component">
                <xsl:with-param name="component" select="$doc//component[@id=$this_component_id]"/>
                <xsl:with-param name="target" select="$this_target"/>
                <xsl:with-param name="query" select="$query//component[@id=$this_component_id]"/>
                <xsl:with-param name="epc_size_state" select="$epc_size_state"/>
                <xsl:with-param name="message" select="$query//component[@id=$this_component_id]/@err_code"></xsl:with-param>
            </xsl:call-template>

            <input type="hidden" id="ep_render_component_ids" name="render_component_ids">
                <xsl:attribute name="value"><xsl:value-of select="$this_component_id"/></xsl:attribute>
            </input>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>
